/**
 * 
 */
package com.gwcloud.leetcode.hot100;

import java.util.Arrays;

/**
 * @ClassName   : Leetcode581 
 * @author      : ganwei 
 * @date        : 2020年5月3日
 * @Description : Leetcode581 最短无序连续子数组
 */
public class Leetcode0581 {
    public int findUnsortedSubarray(int[] nums) {
    	int[] snums = nums.clone();
    	Arrays.sort(snums);
    	int start = nums.length, end = 0;
    	for(int i = 0; i < nums.length; i++){
    		if(snums[i] != nums[i]){
    			start = Math.min(start, i);
    			end = Math.max(i, end);
    		}
    	}
    	return end - start >= 0 ? end - start + 1 : 0;
    }
    
    public static void main(String args[]){
    	Leetcode0581 l581 = new Leetcode0581();
    	int[] nums = {2, 6, 4, 8, 10, 9, 15};
    	System.out.println(l581.findUnsortedSubarray(nums));
    }
    
}
